package com.jims.master.modules.nurse.service;

import com.jims.master.modules.finance.dao.InpBillDetailDao;
import com.jims.master.modules.finance.entity.InpBillDetail;
import com.jims.master.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jims.master.common.service.CrudService;
import com.jims.master.modules.nurse.entity.NurseReturnFeeApply;
import com.jims.master.modules.nurse.dao.NurseReturnFeeApplyDao;

import java.util.Date;
import java.util.List;

/**
 * 护士退费申请Service
 * @author liguoxian
 * @version 2017-07-27
 */
@Service
@Transactional(readOnly = true)
public class NurseReturnFeeApplyService extends CrudService<NurseReturnFeeApplyDao, NurseReturnFeeApply> {

    @Autowired
    private InpBillDetailDao inpBillDetailDao;

    /**
     * 护士查询未退费的收费数据
     * @param inpBillDetail
     * @return
     */
    public List<InpBillDetail> findNotReturnList(InpBillDetail inpBillDetail) {
        return dao.findNotReturnList(inpBillDetail);
    }

    /**
     * 护士查询已退费的收费数据
     * @param inpBillDetail
     * @return
     */
    public List<InpBillDetail> findReturnList(InpBillDetail inpBillDetail) {
        return dao.findReturnList(inpBillDetail);
    }

    /**
     * 病人退费待确认数据
     * @param inpBillDetail
     * @return
     */
    public List<InpBillDetail> findNotConfirmReturnList(InpBillDetail inpBillDetail) {
        return dao.findNotConfirmReturnList(inpBillDetail);
    }

    /**
     * 批量保存
     * @param nurseReturnFeeApplys
     * @return
     */
    @Transactional(readOnly = false)
    public int saveBatch(List<NurseReturnFeeApply> nurseReturnFeeApplys) {
        int i = 0;
        if(nurseReturnFeeApplys != null && nurseReturnFeeApplys.size() > 0) {
            for(NurseReturnFeeApply returnFeeApply : nurseReturnFeeApplys) {
                returnFeeApply.setApplyDate(new Date());
                returnFeeApply.setApplyMan(UserUtils.getUser().getId());
                returnFeeApply.setApplyDept(UserUtils.getUser().getOrgDept().getId());
                i = super.save(returnFeeApply);
            }
        }
        return i;
    }

    /**
     * 确认退费
     * @param id
     * @return
     */
    @Transactional(readOnly = false)
    public int confirm(String id) {
        NurseReturnFeeApply nurseReturnFeeApply = dao.get(id);
        nurseReturnFeeApply.setConfirmDate(new Date());
        nurseReturnFeeApply.setConfirmMan(UserUtils.getUser().getId());
        super.save(nurseReturnFeeApply);
        InpBillDetail billDetail = inpBillDetailDao.get(nurseReturnFeeApply.getInpBillId());
        billDetail.setInpId(billDetail.getId());
        billDetail.setCosts(0 - billDetail.getItemPrice() * nurseReturnFeeApply.getReturnNum());
        billDetail.setCharges(0 - billDetail.getItemPrice() * nurseReturnFeeApply.getReturnNum());
        billDetail.setId(null);
        billDetail.preInsert();
        return inpBillDetailDao.insert(billDetail);
    }
}